//-----------------------------------------------------------------------
// <copyright file="UpdateIdentity.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Updates the Identity which a Win32Service uses for execution.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.ServiceProcess
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Updates the Identity which a Win32Service uses for execution.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<ServiceProcess.UpdateIdentity ServiceName="serviceName" Identity="identity" Password="password" StartupMode="startupMode" />]]></code>
    /// <para>where:</para>
    /// <para><i>serviceName (Required)</i></para>
    /// <para>The short name that identifies the service to the system. This service must exist.</para>
    /// <para><i>identity (Required)</i></para>
    /// <para>The new identity that the specified service will execute under.</para>
    /// <para><i>password (Required)</i></para>
    /// <para>The password for the specified identity.</para>
    /// <para><i>startupMode</i></para>
    /// <para>The start up mode of the specified service.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <ServiceProcess.UpdateIdentity
    ///             ServiceName="W3SVC" 
    ///             Identity="myDomain\myUser"
    ///             Password="myPassword" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class UpdateIdentity : TaskBase
    {
        private string serviceName;
        private string identity;
        private string password;
        private string startupMode = String.Empty;

        /// <summary>
        /// The short name that identifies the service to the system.
        /// </summary>
        /// <value>The short name that identifies the service to the system, e.g. "W3SVC". This service must exist on the local computer.</value>
        [Required]
        public string ServiceName
        {
            get
            {
                return (this.serviceName ?? String.Empty);
            }

            set
            {
                this.serviceName = value;
            }
        }

        /// <summary>
        /// The new identity that the specified service will execute under.
        /// </summary>
        /// <value>The new identity that the specified service will execute under.</value>
        [Required]
        public string Identity
        {
            get
            {
                return (this.identity ?? String.Empty);
            }

            set
            {
                this.identity = value;
            }
        }

        /// <summary>
        /// The password for the specified identity.
        /// </summary>
        /// <value>The password for the specified identity.</value>
        [Required]
        public string Password
        {
            get
            {
                return (this.password ?? String.Empty);
            }

            set
            {
                this.password = value;
            }
        }

        /// <summary>
        /// The start up mode of the specified service.
        /// </summary>
        /// <value>Can be any of:
        /// "Automatic"
        /// "Manual"
        /// "Disabled"
        /// </value>
        public string StartupMode
        {
            get
            {
                return (this.startupMode ?? String.Empty);
            }

            set
            {
                this.startupMode = value;
            }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code
            bool serviceExists = false;

            Log.LogMessageFromResources("Win32Service.ChangeIdentity", this.serviceName);
            System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(this.serviceName);
            try
            {
                string status = sc.Status.ToString();
                serviceExists = true;
            }
            catch (System.ComponentModel.Win32Exception)
            {
                Log.LogMessageFromResources("Win32Service.NotFound", this.serviceName);
            }

            if (serviceExists)
            {
                Log.LogMessageFromResources("Win32Service.Status", this.serviceName, sc.Status.ToString());
                Microsoft.Sdc.Tasks.Configuration.ServiceProcess.ServiceController.SetIdentity(this.serviceName, this.identity, this.password);

                if (this.startupMode != null && this.startupMode.Length != 0)
                {
                    Microsoft.Sdc.Tasks.Configuration.ServiceProcess.ServiceController.SetStartupMode(this.serviceName, this.startupMode);
                }

                Log.LogMessageFromResources(MessageImportance.Low, "Win32Service.IdentityChanged", this.serviceName, this.identity);
            }
            #endregion
        }
    }
}

